<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
	    	<el-row>
				<el-col :span="12">			
				<el-form-item label="租户名称" prop="tenantName">
					<el-input v-model="dataForm.tenantName" placeholder="租户名称"></el-input>
				</el-form-item>
				
				<el-form-item label="用户名" prop="userName">
					<el-input :disabled="disabeld" v-model="dataForm.userName" placeholder="用户名"></el-input>
				</el-form-item>		
				
				<el-form-item label="密码" prop="password">
					<el-input v-model="dataForm.password" placeholder="密码"></el-input>
				</el-form-item>				
				<el-form-item label="联系人电话" prop="mobile">
					<el-input v-model="dataForm.mobile" placeholder="联系人电话"></el-input>
				</el-form-item>
				
			</el-col>
			
			<el-col :span="12">
				<el-form-item prop="packageList" label="租户套餐">
						<el-select v-model="dataForm.packageId" placeholder="租户套餐" style="width: 100%">
							<el-option v-for="packageTenant in packageList" :key="packageTenant.id" :label="packageTenant.name" :value="packageTenant.id"></el-option>
						</el-select>
					</el-form-item>
				<el-form-item prop="status" label="状态">
						<ma-dict-radio v-model="dataForm.status" dict-type="user_status"></ma-dict-radio>
					</el-form-item>
					
				<el-form-item prop="expirationTime" label="过期时间">
					<el-date-picker
						v-model="dataForm.expirationTime"
						type="date"
						placeholder="过期时间"
						size="default"
					/>
					</el-form-item>
					
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { tenantApi, tenantSubmitApi } from '@/api/sys/tenant'
import { tenantPackageListApi } from '@/api/sys/tenantPackage'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	packageId: '',
	tenantName: '',
	status: '',
	mobile: '',
	userId: '',
	userName:'',
	password:'',
	expirationTime:''

})
	
const packageList = ref<any[]>([])
const disabeld = ref(false);
const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getTenant(id)
		disabeld.value = true;
	}
	getPackageList()
}

const getTenant = (id: number) => {
	tenantApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}
// 获取角色列表
const getPackageList = async () => {
	let res = await tenantPackageListApi()
	packageList.value = res.data
}
const dataRules = ref({
	userName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	tenantName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	mobile: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	packageId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	status: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	expirationTime: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		tenantSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
